<?php
declare (strict_types=1);

namespace app\admin\logic\market\advert;

use app\admin\model\market\advert\AdvertLink as AdvertLinkModel;
use app\common\exception\LogicException;
use app\course\model\base\admin\Course;

/**
 * 广告图-内容管理模型
 */
class AdvertLink extends AdvertLinkModel
{
    /**
     * 获取列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-25
     *
     * @return   [type]                         [description]
     */
    public function getList(array $map = [])
    {
        $list = $this->where($map)->with(['imageUrl'])->order('sort')->select();
        foreach ($list as $link) {
            $link = $this->parseLinkDataInfo($link);
        }

        return $list;
    }

    /**
     * 获取信息
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-26
     *
     * @param int $link_id [description]
     * @return   [type]                                  [description]
     */
    public function getLink(int $link_id)
    {
        $link = $this->with(['imageUrl'])->where('id', $link_id)->findOrFail();

        return $this->parseLinkDataInfo($link);
    }

    /**
     * 解析绑定的data数据信息
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-26
     *
     * @param    [type]                         $link [description]
     * @return   [type]                               [description]
     */
    protected function parseLinkDataInfo($link)
    {
        if ($link->getAttr('link_type') == 1 && in_array($link->getAttr('link_data_type'), ['video', 'live', 'live_small', 'classes'])) {
            $link->link_data_info = app(Course::class)->where('id', $link->link_data_id)->field(['cover', 'title'])->with(['coverUrl'])->find();
        }

        return $link;
    }

    /**
     * 新建广告内容
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-26
     *
     * @param    [type]                         $data [description]
     * @return   [type]                               [description]
     */
    public function createLink(array $data)
    {
        // 验证数据
        $this->getValidate()->scene('create')->failException()->check($data);

        $save = [
            'advert_id' => $data['advert_id'],
            'image'     => $data['image'],
            'link_type' => $data['links']['link_type'],
        ];

        $save = $this->appendSaveData($save, $data);

        if (!$this->save($save)) {
            throw new LogicException('添加失败');
        }

        return $this;
    }

    protected function appendSaveData($save, $data)
    {
        // 跳转类型
        switch ($save['link_type']) {
            case '1':
                $save['link_data_type'] = $data['links']['link_data_type'];
                $save['link_data_id']   = $data['links']['link_data_id'];
                $save['link_href']      = '';
                break;
            case '2':
                $save['link_data_type'] = 0;
                $save['link_data_id']   = 0;
                $save['link_href']      = $data['links']['link_href'];
                break;
        }

        return $save;
    }

    /**
     * 编辑广告内容
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-26
     *
     * @param int   $link_id [description]
     * @param array $data    [description]
     * @return   [type]                                  [description]
     */
    public function editLink(int $link_id, array $data)
    {
        $advertLink = $this->where('id', $link_id)->findOrFail();

        $check       = $data;
        $check['id'] = $link_id;
        // 验证数据
        $this->getValidate()->scene('edit')->failException()->check($check);
        $save = [
            'image'     => $data['image'],
            'link_type' => $data['links']['link_type'],
        ];
        $save = $this->appendSaveData($save, $data);
        foreach ($save as $k => $val) {
            $advertLink->$k = $val;
        }
        if (!$advertLink->save()) {
            throw new LogicException('修改失败');
        }

        return $this;
    }

    /**
     * 删除广告内容
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-26
     *
     * @param    [type]                         $id [description]
     * @return   [type]                             [description]
     */
    public function deleteLink(int $link_id)
    {
        $link = $this->where('id', $link_id)->findOrFail();

        if (!$link->delete()) {
            throw new LogicException("删除失败");
        }
    }

}
